package com.tutorial.demo.goods.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tutorial.demo.goods.pojo.Tbgoods;
import com.tutorial.demo.goods.service.TbgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class TbgoodsController {
    @Autowired
    private TbgoodsService tbgoodsService;
    private static Gson gson = new Gson();
    @RequestMapping("/goods/findAll")
    @ResponseBody
    public List<Tbgoods> findAll(){
        return tbgoodsService.findAll();
    }

    @RequestMapping("/goods/findParentsById")
    @ResponseBody
    public List<Tbgoods> findParentsById(@RequestBody String body){
        Map<String,Integer> map = gson.fromJson(body,new TypeToken<Map<String,Integer>>(){}.getType());
        Integer id = map.get("id");
        return tbgoodsService.query(id);
    }
}
